import { Component, OnInit } from "@angular/core";
import { DatatableComponent } from "@swimlane/ngx-datatable/release";
import { JsonApiService } from "app/core/api/json-api.service";
import { Router, ActivatedRoute } from "@angular/router";
import { config } from '../../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: "sa-RFID-configure",
	templateUrl: "./RFID-configure.component.html",
	styleUrls: ["./RFID-configure.css"],
})
export class RFIDConfigureComponent implements OnInit {
	tableData: any = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //分页每页条数
	searchKey: string = ""; //搜索keyword

	formType: string = ""; // 表单类型
	fileUp: string = '';
	fileToUpload: File = null; //上传文件回调参数

	// 提交的表单
	RFIDForm: any = {
		rfid: "",
		communityName: "",
		provinceId: '',
		cityId: '',
		areaId: '',
		isEnable: 1,
	};

	validatorOptions = {
		excluded: [":disabled"],
		feedbackIcons: {
			valid: "glyphicon glyphicon-ok",
			invalid: "glyphicon glyphicon-remove",
			validating: "glyphicon glyphicon-refresh",
		},
		fields: {
			rfid: {
				validators: {
					notEmpty: {
						message: "请输入RFID",
					},
				},
			},
			communityName: {
				validators: {
					notEmpty: {
						message: "请输入小区名称",
					},
				},
			},
			provinceId:{
				trigger:"change",
				validators: {
					notEmpty: {
						message: '请选择省'
					}
				}
			},
			cityId:{
				trigger:"change",
				validators: {
					notEmpty: {
						message: '请选择市'
					}
				}
			},
			areaId:{
				trigger:"change",
				validators: {
					notEmpty: {
						message: '请选择区'
					}
				}
			},
		},
	};

	constructor(
		private jsonApiService: JsonApiService,
		private router: Router,
		private activatedRoute: ActivatedRoute
	) {
		activatedRoute.queryParams.subscribe((queryParams) => {});
	}
	//初始化钩子函数
	ngOnInit() {
		this.load();
		this.initdata();
	}
	//刷新
	refresh() {
		this.load();
	}

	//表格搜索
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				searchKey: this.searchKey,
			},
		};
		let reqdata = "gridPager=" + JSON.stringify(data);
		this.jsonApiService
			.http("environmentalAdministration/communityAllocationFind.html", reqdata)
			.subscribe((data) => {
				if (data.isSuccess) {
					this.tableData = data.exhibitDatas;
					this.totalCount = data.recordCount;
				}
			});
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	/**
	 * 新增和编辑
	 */
	newCompany(modal) {
		this.formType = "new";
		$("#RFIDForm").data("bootstrapValidator").resetForm();
		this.RFIDForm = {
			rfid: "",
			communityName: "",
			provinceId: '',
			cityId: '',
			areaId: '',
			isEnable: 1,
		};
		modal.show();
	}
	//  编辑的弹框打开
	editCompany(row, modal) {
		this.formType = "update";

		this.RFIDForm.id = row.id;
		this.RFIDForm.rfid = row.rfid;
		this.RFIDForm.communityName= row.communityName;
		this.RFIDForm.provinceId= row.provinceId;
		this.RFIDForm.cityId= row.cityId;
		this.RFIDForm.areaId= row.areaId;
		this.RFIDForm.isEnable= row.isEnable;

		this.provinceChange(this.RFIDForm.provinceId == null ? "" : this.RFIDForm.provinceId);
		this.cityChange(this.RFIDForm.cityId == null ? "" : this.RFIDForm.cityId);
		this.townChange(this.RFIDForm.areaId == null ? "" : this.RFIDForm.areaId);
		$("#RFIDForm").data("bootstrapValidator").resetForm();
		// this.RFIDForm = JSON.parse(JSON.stringify(row));
		modal.show();
	}
	isEnableChange(status) {
		this.RFIDForm.isEnable = status;
	}
	// 关闭
	companyClose(modal) {
		$("#RFIDForm").data("bootstrapValidator").resetForm();
		modal.hide();
		this.RFIDForm = {
			rfid: "",
			communityName: "",
			provinceId: '',
			cityId: '',
			areaId: '',
			isEnable: 1,
		};
	}
	// 删除RFID
	deleteCompany(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata= {id:row.id}
			this.jsonApiService.http_obj('environmentalAdministration/communityAllocationRemove.html', reqdata).subscribe((data) => {
				this.jsonApiService.message(data.message);
				// 如果添加成功
				if (data.success) {
					this.load();
				}
			});
		})
	}
	// 提交
	companySubmit(modal) {
		$("#RFIDForm").bootstrapValidator("validate"); //开启验证
		let isValid = $("#RFIDForm").data("bootstrapValidator").isValid(); //验证是否通过
		console.log(this.RFIDForm);
		let url = "environmentalAdministration/communityAllocationInsertion.html";

		let reqdata = JSON.parse(JSON.stringify(this.RFIDForm))
		if (isValid) {
			this.jsonApiService.http_obj(url, reqdata).subscribe((data) => {
				this.jsonApiService.message(data.message);
				// 如果添加成功
				if (data.success) {
					this.load();
					this.companyClose(modal);
				}
			});
		}
	}
	// 显示导入的弹框
	showImportModal(modal) {
		modal.show();
	}
	// 关闭导入的弹框
	cancelUp(val) {
		val.hide();
		this.fileToUpload = null;
	}
	// 清空input type=file输入框的值
	clearFile() {
		this.fileUp = '';
	}
	//文件上传change回调
	fileChange(files: FileList) {
		this.fileToUpload = files.item(0);
		const isEXCEL = this.fileToUpload.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || this.fileToUpload.type === "application/vnd.ms-excel";
		if (!isEXCEL) {
			this.jsonApiService.message('上传文件只能是 excel格式!');
			this.fileToUpload = null;
			return;
		}
	}
	//导入submit
	submitData(val) {
		let postParam = new FormData();
		postParam.append('file', this.fileToUpload);
		if (!this.fileToUpload) {
			this.jsonApiService.message('请上传文件')
			return false;
		}
		this.jsonApiService.form_http("environmentalAdministration/upload.html", postParam).subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				val.hide();
				this.fileToUpload = null;
				this.load();
			}
		})
	}

	areaData: any[] = []; //省市县数据
	provinceNameArr: any[] = []; //省数据
	cityNameArr: any[] = []; //城市数据
	townNameArr: any[] = []; //县数据
	//拉取省市县数据
	initdata() {
		this.jsonApiService.http("company/regionList.html", null).subscribe(data => {
			if(data.message) {
				this.areaData = data.data;
				for(let item of this.areaData) {
					if(item.level === 2) {
						this.provinceNameArr.push(item);
					}
				}
			} else {
				this.jsonApiService.message(data.message);
			}
		})
	}
	//省份change回调事件
	provinceChange(value) {
		this.cityNameArr = [];
		this.townNameArr = [];
		this.RFIDForm.provinceId = value;
		for(let item of this.areaData) {
			if(this.RFIDForm.provinceId === item.parentId) {
				this.cityNameArr.push(item);
			}
		}
		$("#RFIDForm").data('bootstrapValidator').resetForm();
	}
	//城市change回调事件
	cityChange(value) {
		this.townNameArr = [];
		this.RFIDForm.cityId = value;
		for(let item of this.areaData) {
			if(this.RFIDForm.cityId === item.parentId) {
				this.townNameArr.push(item);
			}
		}
		$("#RFIDForm").data('bootstrapValidator').resetForm();
	}
	townChange(value) {
		this.RFIDForm.areaId = value;
		$("#RFIDForm").data('bootstrapValidator').resetForm();
	}
}
